# python -m project_root.api.api_home
from .api_token import Token 
from util.time_util import timestran
from util.log_util import logs_info,logs_error,logs_debug
import requests


class Api_Home(Token):


    def get_topdata(self,id=1):
        try:
            url = 'http://hxzhan.rtcloud.com:32111/tm/home/overview'
            keys = ['app_scen_count','gateway_count','device_count','alarm_device_count','app_scen_city_count','gateway_online_count','gateway_offline_count',
                    'device_online_count','device_offline_count','alarm_device_yesterday']
            response = requests.post(url=url,headers=self.get_header())
            assert response.status_code == 200
            logs_info(f'调用接口 get_topdata 成功 id ={id}')
            data = response.json()['data']
            values = []
            if id == 1:
                keys = keys[:4]
            elif id == 2:
                keys = keys[4:]
            for key in keys:
                values.append(data[key])
            return values
        except Exception as e:
            logs_error("调用接口 get_topdata 失败 ")
            

    def get_alarmlist(self):
        try:
            url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/list'
            json = {
                'limit':6,
                'page':1
            }
            keys = ['alarm_at','device_name','app_scen_name','desc','level','return_status']
            response = requests.post(url=url,headers=self.get_header(),json=json)
            assert response.status_code == 200
            logs_info("调用接口 get_alarmlist 成功 ")
            values = []
            for data in response.json()['data']['list']:
                for key in keys:
                    if key == 'alarm_at':
                        values.append(timestran(data[key]))
                    elif key == 'level':
                        if data[key] == 1:
                            values.append('预警')
                        elif data[key] == 2:
                            values.append('报警')
                    elif key == 'return_status':
                        if data[key] == 1:
                            values.append('已复归')
                        elif data[key] == 2:
                            values.append('未复归')
                    else:
                        values.append(data[key])
            return values
        except Exception as e:
            logs_error("调用接口 get_alarmlist 失败")
        

    def get_site_number(self):
        url = 'http://hxzhan.rtcloud.com:32111/tm/home/station/overview'
        json = {
            'limit':12,
            'page':1,
            'status':0
        }
        keys = ['total','online','offline','part_online']
        response = requests.post(url=url,headers=self.get_header(),json=json)
        values = []
        data = response.json()['data']['nums']
        for key in keys:
            values.append(data[key])
        return values

    def get_site_data(self):
        values = []
        for status in range(0,4):
            url = 'http://hxzhan.rtcloud.com:32111/tm/home/station/overview'
            json = {
                'limit':12,
                'page':1,
                'status':status
            }
            keys = ['app_scen_name','gateway_online_status','top_region_name','child_device_count','alarm_device_count','highest_temp','lowest_temp']
            response = requests.post(url=url,headers=self.get_header(),json=json)
            sites = response.json()['data']['list']
            for site in sites:
                for key in keys:
                    if key == 'gateway_online_status':
                        if site[key] == 1:
                            values.append('在线')
                        elif site[key] == 2:
                            values.append('离线')
                        elif site[key] == 3:
                            values.append('部分离线')
                    else:
                        values.append(str(site[key]))
        return values
    
    def get_foot_number(self):
        foot_numbers = []
        for status in range(0,4):
            url = 'http://hxzhan.rtcloud.com:32111/tm/home/station/overview'
            json = {
                'limit':12,
                'page':1,
                'status':status
            }
            response = requests.post(url=url,headers=self.get_header(),json=json)
            foot_number = response.json()['data']['count']
            foot_numbers.append(foot_number)
        return foot_numbers



# a = Api_Home()
# print(a.get_foot_number())
